package cn.wangkai.peanut.action.util;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.wangkai.peanut.db.util.BeanUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

public class BaseAction {
	private static final Log log = LogFactory.getLog(BaseAction.class);

	public static String getJsonMessage(boolean succees, String msg) {
		JSONObject json = new JSONObject();
		try {
			json.put("succees", new Boolean(succees));
			json.put("msg", msg);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return json.toJSONString();
	}

	public static Object getRequestBean(HttpServletRequest request,
			@SuppressWarnings("rawtypes") Class type) throws SQLException {
		HashMap<String, Object> rsh = new HashMap<String, Object>();
		BeanUtil beanUtil = new BeanUtil();
		@SuppressWarnings("unchecked")
		Map<String, Object> params = request.getParameterMap();
		Iterator<Map.Entry<String, Object>> iter = params.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iter.next();
			String key = entry.getKey().toString();
			Object obj = entry.getValue();
			rsh.put(key, obj);
		}
		return beanUtil.toBean(rsh, type);

	}
}
